package com.itita.ww2.handler.requesthandler.building;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.enums.PushCode;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.game.user.UserBuildingManager.BuildingType;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.DevKeyUtil;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 固定建筑升级完成
 * @author NorthLan
 *
 */
public class BaseBDUpgradeComplete implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		//
		ISFSObject backObj = SFSObject.newInstance();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));
		//
		String key = params.getUtfString("devKey");
		String bdCode = DevKeyUtil.splitKey(key, 1);
		BuildingType bdType = BuildingType.codeValueOf(bdCode);

		if (!userProfile.getBDManager().upgradeComplete(bdType)) {
			throw new WW2Exception(WW2ExceptionCode.INVALID_OPT);
		}

		// 推送
		ISFSObject pushObj = SFSObject.newInstance();
		pushObj.putUtfString("key", key);
		pushObj.putUtfString("code", bdCode);
		pushObj.putInt("lv", userProfile.getBDManager().getBuildingByType(bdType).getLevel());
		MessageHandler.getInstance().pushGB(PushCode.GAME_BUILDING_UPGRADE_COMPLETE, pushObj, user);
		//
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
